﻿using DataAccess.Models;

using Solr;
using Solr.Commons;
using Solr.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.Solr
{
   
    public class SolrProductDetails
    {
        static ISolrResponseParser<NamedList, ResponseHeader> binaryResponseHeaderParser = new BinaryResponseHeaderParser();
        static IObjectDeserializer<ProductDetails> exampleDeserializer = new ProductDeserializer();
        static ISolrResponseParser<NamedList, QueryResults<ProductDetails>> binaryQueryResultsParser = new BinaryQueryResultsParser<ProductDetails>(exampleDeserializer);

        static ISolrResponseParser<NamedList, IList<GroupQueryResults<ProductDetails>>> GroupByeParser = new BinaryGroupQueryResultsParser<ProductDetails>(exampleDeserializer);

        static ISolrQueryConnection<NamedList> Productconnection = null;
        static ISolrQueryOperations<NamedList> Productoperations = null;
        static ISolrResponseParser<NamedList, IDictionary<string, IDictionary<string, IList<string>>>> binaryHighlightingParser = new BinaryHighlightingParser();
        private static string SolrPath = System.Configuration.ConfigurationManager.AppSettings["Solr"];
        static string ProductCollectionName = "product";
        public SolrProductDetails()
        {

        }
        static SolrProductDetails()
        {
            if (Productconnection == null && Productoperations == null)
            {

                Productconnection = new SolrQueryConnection<NamedList>() { ServerUrl = SolrPath };
                Productoperations = new SolrQueryOperations<NamedList>(Productconnection) { ResponseWriter = "javabin" };
            }

        }
        /// <summary>
        /// SolrQuestion反序列化器
        /// </summary>
        public class ProductDeserializer : IObjectDeserializer<ProductDetails>
        {
            public IEnumerable<ProductDetails> Deserialize(SolrDocumentList result)
            {
                foreach (SolrDocument doc in result)
                {
                    yield return new ProductDetails()
                    {

                        auctionId = doc["id"] != null ? long.Parse(doc["id"].ToString()) : 0,
                        auctionUrl = doc["auctionUrl"] != null ? doc["auctionUrl"].ToString() : "",
                        biz30day = doc["biz30day"] != null ? int.Parse(doc["biz30day"].ToString()) : 0,
                        couponActivityId = doc["couponActivityId"] != null ? doc["couponActivityId"].ToString() : "",
                        couponAmount = doc["couponAmount"] != null ? float.Parse(doc["couponAmount"].ToString()) : 0,
                        couponTotalCount = doc["couponTotalCount"] != null ? doc["couponTotalCount"].ToString() : "0",
                        //couponLeftCount = doc["couponLeftCount"] != null ? int.Parse(doc["couponLeftCount"].ToString()) : 0,
                        //couponLink = doc["couponLink"] != null ? doc["couponLink"].ToString() : "",
                         couponEffectiveStartTime = doc["couponEffectiveStartTime"] != null ? doc["couponEffectiveStartTime"].ToString() : "",
                          couponEffectiveEndTime = doc["couponEffectiveEndTime"] != null ? doc["couponEffectiveEndTime"].ToString() : "",
                        Introduce = doc["Introduce"] != null ? doc["Introduce"].ToString() : "",
                        pictUrl = doc["pictUrl"] != null ? doc["pictUrl"].ToString() + "_480x480.jpg" : "",
                        //reservePrice = doc["reservePrice"] != null ? float.Parse(doc["reservePrice"].ToString()) : 0,
                        zkPrice = doc["zkPrice"] != null ? float.Parse(doc["zkPrice"].ToString()) : 0,
                        //佣金
                        tkRate = doc["tkRate"] != null ? float.Parse(doc["tkRate"].ToString()) : 0,
                        //Rate = doc["tkRate"] != null ? float.Parse(doc["tkRate"].ToString()) : 0,
                        //最终商品价格
                        Price = doc["zkPrice"] != null ? (decimal.Parse(doc["zkPrice"].ToString()) - decimal.Parse(doc["couponAmount"].ToString())).ToString("F2") : "0",
                        userType = doc["userType"] != null ?int.Parse(doc["userType"].ToString()) : 0,
                        shopTitle =!string.IsNullOrEmpty(doc["shopTitle"].ToString()) ? doc["shopTitle"].ToString() : "淘宝",
                        DataTypeID = doc["DataTypeID"] != null ? int.Parse(doc["DataTypeID"].ToString()) : 0,
                        title = doc["productname"] != null ? doc["productname"].ToString() : "",
                        //佣jin
                        Rate =(float.Parse(doc["tkRate"].ToString())/100)*  float.Parse(doc["prices"].ToString()) 
                       
                        

                    };
                }
            }
        }
        /// <summary>
        /// 通过商品ID查询
        /// </summary>
        /// <param name="ProductSpecRelRid"></param>
        /// <returns></returns>
        public ProductDetails GetProductDetailsByProductInfo(string auctionId)
        {
            ISolrQuery query = new SolrQuery("id:" + auctionId + "");

            var options = new Dictionary<string, ICollection<string>>();

            options.Add("start", new string[] { "0" });
            options.Add("rows", new string[] { "1" });

            var result = Productoperations.Query(ProductCollectionName, "/select", query, options);

            var examples = binaryQueryResultsParser.Parse(result);

            return examples.FirstOrDefault();
        }



    }
}
